package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.DeclarePlan;
import com.cls.business.service.IDeclarePlanService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 *  Controller
 *
 * @author slx
 * @date 2020-12-11 10:39:22
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class DeclarePlanController extends BaseController {

    private final IDeclarePlanService declarePlanService;

    /**
    * 列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/declarePlan")
    public String declarePlanIndex(){
        return MyUtil.view("business/declarePlan/declarePlan");
    }

    /**
    * 新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/declarePlan/declarePlanAdd")
    public String declarePlanAdd(Model model){
        return MyUtil.view("business/declarePlan/declarePlanAdd");
    }

    /**
    * 修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/declarePlan/declarePlanUpdate/{declarePlanId}")
    public String declarePlanUpdate(@PathVariable Long declarePlanId, Model model){
        DeclarePlan declarePlan = declarePlanService.getById(declarePlanId);
        model.addAttribute("declarePlan",declarePlan);
        return MyUtil.view("business/declarePlan/declarePlanUpdate");
    }



    /**
    * 详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/declarePlan/declarePlanDetail/{declarePlanId}")
    public String declarePlanDetail(@PathVariable Long declarePlanId, Model model){
        DeclarePlan declarePlan = declarePlanService.getById(declarePlanId);
        model.addAttribute("declarePlan",declarePlan);
        return MyUtil.view("business/declarePlan/declarePlanDetail");
    }

    @GetMapping("declarePlan")
    @ResponseBody
    @RequiresPermissions("declarePlan:list")
    public ResponseVO getAllDeclarePlans(DeclarePlan declarePlan) {
        return ResponseVO.success(declarePlanService.findDeclarePlans(declarePlan));
    }

    @GetMapping("declarePlan/list")
    @ResponseBody
   /* @RequiresPermissions("declarePlan:list")*/
    public ResponseVO declarePlanList(QueryRequest request, DeclarePlan declarePlan) {
        PageInfo<DeclarePlan> pageInfo =  this.declarePlanService.findDeclarePlans(request, declarePlan);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增", exceptionMessage = "新增失败")
    @PostMapping("declarePlan")
    @ResponseBody
    @RequiresPermissions("declarePlan:add")
    public ResponseVO addDeclarePlan(@Valid DeclarePlan declarePlan) {
        this.declarePlanService.createDeclarePlan(declarePlan);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除", exceptionMessage = "删除失败")
    @PostMapping("declarePlan/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("declarePlan:delete")
    public ResponseVO deleteDeclarePlans(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.declarePlanService.deleteDeclarePlans(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个", exceptionMessage = "删除失败")
    @PostMapping("declarePlan/delete/{declarePlanId}")
    @ResponseBody
    @RequiresPermissions("declarePlan:delete")
    public ResponseVO deleteDeclarePlan(@PathVariable Long declarePlanId) {
        this.declarePlanService.removeById(declarePlanId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改", exceptionMessage = "修改失败")
    @PostMapping("declarePlan/update")
    @ResponseBody
    @RequiresPermissions("declarePlan:update")
    public ResponseVO updateDeclarePlan(DeclarePlan declarePlan) {
        this.declarePlanService.updateDeclarePlan(declarePlan);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "导出", exceptionMessage = "导出失败")
    @PostMapping("declarePlan/excel")
    @ResponseBody
    @RequiresPermissions("declarePlan:export")
    public void export(QueryRequest queryRequest, DeclarePlan declarePlan, HttpServletResponse response) {
        List<DeclarePlan> declarePlans = this.declarePlanService.findDeclarePlans(queryRequest, declarePlan).getList();
        ExcelKit.$Export(DeclarePlan.class, response).downXlsx(declarePlans, false);
    }
}
